package com.apollow.oschina.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.apollow.oschina.R;
import com.apollow.oschina.base.Global;
import com.apollow.oschina.bean.BlogBean;
import com.apollow.oschina.bean.GeneralBlogDetailBean;
import com.apollow.oschina.protocol.BlogProtocol;
import com.apollow.oschina.ui.WebViewActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Dragon on 217/1/13.
 */

public class BlogAdapter extends RecyclerView.Adapter{
    public Activity mActivity;
    public BlogProtocol mProtocol;
    public List<BlogBean> blogBeen;

    public BlogAdapter(Activity activity, BlogProtocol protocol) {
        mActivity = activity;
        mProtocol = protocol;
    }
    public void setBlogBeen(List<BlogBean> blogBeen) {

        this.blogBeen = blogBeen;
        notifyDataSetChanged();
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = Global.inflate(R.layout.item_rv_blog, parent);
        ViewHolder viewHolder = new ViewHolder(itemView);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder holderview = (ViewHolder) holder;
        holderview.setData(blogBeen.get(position));
    }
    @Override
    public int getItemCount() {
        return blogBeen==null?0:blogBeen.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_title_blog)
        TextView mTvTitleBlog;
        @BindView(R.id.tv_body_blog)
        TextView mTvBodyBlog;
        @BindView(R.id.tv_author_blog)
        TextView mTvAuthorBlog;
        @BindView(R.id.tv_data_blog)
        TextView mTvDataBlog;
        @BindView(R.id.tv_discuss_blog)
        TextView mTvDiscussBlog;
        BlogBean blogBean;
        public TextView mTv1;
        public TextView mTv2;
        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final String newsID=String.valueOf(blogBean.getId())+",";
                    String ReadItemId=Global.getReadedNews("");
                    mTv1 = (TextView) v.findViewById(R.id.tv_title_blog);
                    mTv2 = (TextView) v.findViewById(R.id.tv_body_blog);
                    if (!ReadItemId.contains(newsID)){
                        ReadItemId+=newsID;
                        Global.putReadedews(ReadItemId);
                        mTv1.setTextColor(Color.parseColor("#40AA53"));
                        mTv2.setTextColor(Color.parseColor("#40AA53"));
                    }else{
                        mTv1.setTextColor(Color.parseColor("#40AA53"));
                        mTv2.setTextColor(Color.parseColor("#40AA53"));
                    }
                    new Thread(new Runnable() {
                        Intent mIntent=new Intent(mActivity, WebViewActivity.class);
                        @Override
                        public void run() {
                            GeneralBlogDetailBean blogDetail = mProtocol.getBlogDetail(blogBean.getId() + "");
                            mIntent.putExtra("URL",blogDetail.getBlog().getUrl());
                            mActivity.startActivity(mIntent);
                        }
                    }
                    ).start();
                }
            });
        }
        public void setData(BlogBean blogBean) {
            this.blogBean=blogBean;
            mTvTitleBlog.setText(blogBean.getTitle());
            mTvBodyBlog.setText(blogBean.getBody());
            mTvAuthorBlog.setText(blogBean.getAuthor());
            mTvDataBlog.setText(blogBean.getPubDate());
            mTvDiscussBlog.setText(blogBean.getCommentCount() + "");
            String ReadedItemID=Global.getReadedNews("");
            ReadedItemID.contains(String.valueOf(blogBean.getId()));
            if (!ReadedItemID.contains(String.valueOf(blogBean.getId()))){
                mTvTitleBlog.setTextColor(Color.parseColor("#352e2e"));
                mTvBodyBlog.setTextColor(Color.parseColor("#352e2e"));
            }else {
                mTvTitleBlog.setTextColor(Color.parseColor("#40AA53"));
                mTvBodyBlog.setTextColor(Color.parseColor("#40AA53"));
            }
        }
    }
}

